package com.taplinker.client.im.activity.message;

import java.io.File;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taplinker.client.AppConfigContext;
import com.taplinker.client.R;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.core.RecordingManager;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.client.im.protocol.TapLinkerMessage;
import com.taplinker.client.im.rpc.DownloadCommand;
import com.taplinker.core.util.ImageUtil;
import com.taplinker.core.util.TimeUtil;

public class RightAudioMessage extends AbstractMessageView{

	@Override
	public View getMessageView(final Context context, TapLinkerMessage message, View convertView) {
		final AudioMessageHolder audioMessageHolder;
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(MessageTypeEnum.getMessageType(message).getLayoutId(), null);
			audioMessageHolder = new AudioMessageHolder();
			audioMessageHolder.name = (TextView) convertView.findViewById(R.id.name_text_tv);
			audioMessageHolder.head = (ImageView) convertView.findViewById(R.id.head_icon_iv);
			audioMessageHolder.soundTime = (TextView) convertView.findViewById(R.id.content_sound_tv);
			audioMessageHolder.playing = convertView.findViewById(R.id.audio_playing);
			audioMessageHolder.time = (TextView) convertView.findViewById(R.id.time_text_tv);
			audioMessageHolder.state = (TextView) convertView.findViewById(R.id.sender_state_tv);
			convertView.setTag(audioMessageHolder);
		} else {
			audioMessageHolder = (AudioMessageHolder) convertView.getTag();
		}
		audioMessageHolder.name.setText(message.getSendUser());
		if(!TextUtils.isEmpty(message.getIcon())) {
			String headUrl = AppConfigContext.WEB_DISK_ROOT + message.getIcon();
			ImageManager.getInstance().loadImage(headUrl, audioMessageHolder.head, ImageUtil.dip2px(context, 45), ImageUtil.dip2px(context, 45),null);
		}
		
		final String path = getAccessUrl(message);
		new DownloadCommand(path, new Handler.Callback() {
			
			@Override
			public boolean handleMessage(Message msg) {
				final String recPath = (String) msg.obj;
				File file = new File(recPath);
				final int mTime = MediaPlayer.create(context, Uri.fromFile(file)).getDuration();
				int seconds = (int)(mTime/1000);
				if(seconds == 0) seconds = 1;
				audioMessageHolder.soundTime.setText(seconds + "\"");
				// 更改录音气泡长度
				final Drawable right = audioMessageHolder.soundTime.getCompoundDrawables()[2];
				audioMessageHolder.soundTime.setWidth(ImageUtil.dip2px(context, 80) + 2 * seconds);
				audioMessageHolder.soundTime.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if(!RecordingManager.getInstance().isPlaying()) {
							audioMessageHolder.soundTime.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
							audioMessageHolder.playing.setVisibility(View.VISIBLE);
							RecordingManager.getInstance().playRec(recPath, mTime, new RecordingManager.CallBack() {
								
								@Override
								public void doAfterOver() {
									audioMessageHolder.soundTime.setCompoundDrawablesWithIntrinsicBounds(null, null, right, null);
									audioMessageHolder.playing.setVisibility(View.GONE);
								}
							});
						}
					}
				});
				ImDataCenter.getInstance().noticeListener(ImDataCenter.MESSAGE_FINISH);
				return false;
			}
		}).execute();
		
		audioMessageHolder.state.setText("");
		audioMessageHolder.state.setPadding(
				ImageUtil.dip2px(context, 5),
				ImageUtil.dip2px(context, 3),
				ImageUtil.dip2px(context, 5),
				ImageUtil.dip2px(context, 3));
		audioMessageHolder.state.setBackgroundResource(R.drawable.message_state_bg);
		if (message.isUnarrived()){
			// 切换未送到消息图标提示
			audioMessageHolder.state.setTextColor(Color.RED);
			audioMessageHolder.state.setPadding(0, 0, 0, 0);
			audioMessageHolder.state.setBackgroundResource(R.drawable.msg_warning);
		} else if (message.getSendTime()==null) {
			//audioMessageHolder.state.setText("发送中");
			//audioMessageHolder.state.setTextColor(0xff4dae4b);
		} else if (message.getRead()) {
			//audioMessageHolder.state.setText("已读");
			audioMessageHolder.state.setBackgroundResource(R.drawable.input_read);
		//	audioMessageHolder.state.setTextColor(0xff4dae4b);
		} else {
			//audioMessageHolder.state.setText("已送达");
			//audioMessageHolder.state.setTextColor(0xffff8a00);
			audioMessageHolder.state.setBackgroundResource(R.drawable.input_songda);
		}
		if(message.getSendTime() != null) {
			audioMessageHolder.time.setText(TimeUtil.timeFormat(message.getSendTime()));
		}
		return convertView;
	}
	
	@Override
	public String getAccessUrl(TapLinkerMessage msg) {
		String url = "";
		if(msg.getLocalUrl()!=null && msg.getLocalUrl().trim().length()>0 ) {
			url = msg.getLocalUrl();
		} else if (msg.getContent()!=null && msg.getContent().trim().length()>0 ) {
			url = AppConfigContext.WEB_DISK_ROOT + msg.getContent();
		} else {
			url = msg.getLocalUrl();
		}
		return url;
	}
	
	class AudioMessageHolder {
		TextView name;		// 用户名
		ImageView head;		// 头像
		TextView soundTime;	// 语音时长
		View playing;
		TextView time;		// 时间
		TextView state;		// 发送状态
	}

}
